﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace WindowsFormsApplication1
{
    public partial class BuscarPaciente : Form
    {
        Form p;

        public BuscarPaciente(Form p)
        {
            InitializeComponent();
            this.p = p;
        }

        private void txtNombre_TextChanged(object sender, EventArgs e)
        {
            if (txtNombre.Text.Length > 0)
            {

                EmbeddedDatabase db = EmbeddedDatabase.Instance;

                ///split keywords and form query...
                String query = "SELECT * FROM Paciente WHERE";
                char[] space = { ' ' };
                String[] keywords = txtNombre.Text.Split(space);
                for (int i = 0; i < keywords.Length; i++)
                {
                    if (keywords[i].Length == 0) continue;
                    if (i > 0) query += " OR";
                    query += " Nombre like '" + keywords[i] + "%'";
                    query += "OR ApellidoPaterno like '" + keywords[i] + "%'";
                    query += "OR ApellidoMaterno like '" + keywords[i] + "%'";
                }

                SqlCeResultSet r = db.GetResultSetForCustomQuery(query);

                lstPacientes.Items.Clear();
                while (r.Read())
                {
                    ItemObject o = new ItemObject(r["Nombre"] + " " + r["ApellidoPaterno"] + " " + r["ApellidoMaterno"] + " (" + edad((DateTime)r["Fecha_nac"]) + " años)", r["IdPaciente"]);
                    lstPacientes.Items.Add(o);
                }
            }
            else
            {
                EmbeddedDatabase db = EmbeddedDatabase.Instance;

                SqlCeResultSet r = db.GetResultSetForCustomQuery("SELECT * FROM Paciente");

                lstPacientes.Items.Clear();
                while (r.Read())
                {
                    ItemObject o = new ItemObject(r["Nombre"] + " " + r["ApellidoPaterno"] + " " + r["ApellidoMaterno"] + " (" + edad((DateTime)r["Fecha_nac"]) + " años)", r["IdPaciente"]);
                    lstPacientes.Items.Add(o);
                }
            }
        }

        private int edad(DateTime fecha)
        {
            return DateTime.Now.Subtract(fecha).Days / 360;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Hide();
            p.Show();
        }

        private void BuscarPaciente_Load(object sender, EventArgs e)
        {
            p.Hide();
            this.Show();

            txtNombre_TextChanged(null, null);
        }

        private void btnVerHistorial_Click(object sender, EventArgs e)
        {
            VerHistorial();
        }

        private void VerHistorial()
        {
            //MessageBox.Show(((ItemObject)lstPacientes.SelectedItem).ValueOfKey.ToString());
            if (lstPacientes.SelectedIndex != -1)
            {
                VerHistorialMedico a = new VerHistorialMedico(this, Int16.Parse(((ItemObject)lstPacientes.SelectedItem).ValueOfKey.ToString()));
                a.ShowDialog(this);
                txtNombre_TextChanged(null, null);
            }
        }

        private void lstPacientes_DoubleClick(object sender, EventArgs e)
        {
            VerHistorial();
        }
    }
}
